1 /*
2 Copyright: Marcelo S. N. Mancini (Hipreme|MrcSnm), 2018 - 2021
3 License:   [https://creativecommons.org/licenses/by/4.0/|CC BY-4.0 License].
4 Authors: Marcelo S. N. Mancini
5 
6 	Copyright Marcelo S. N. Mancini 2018 - 2021.
7 Distributed under the CC BY-4.0 License.
8    (See accompanying file LICENSE.txt or copy at
9 	https://creativecommons.org/licenses/by/4.0/
10 */
11 module hip.hiprenderer.backend.gl.glframebuffer;
12 version(OpenGL):
13 
14 import hip.error.handler;
15 import hip.hiprenderer.renderer;
16 import hip.hiprenderer.framebuffer;
17 import hip.hiprenderer.shader;
18 import hip.hiprenderer.backend.gl.gltexture;
19 
20 
21 class Hip_GL3_FrameBuffer : IHipFrameBuffer
22 {
23     ///Texture to be returned. It is filled with the opengl framebuffer contents
24     Hip_GL3_Texture retTexture;
25     uint rbo;
26     uint fbo;
27     uint texture;
28     
29 
30     this(int width, int height)
31     {
32         create(width, height);
33     }
34     void create(uint width, uint height)
35     {
36         //Objects initialization
37         glCall(() => glGenFramebuffers(1, &this.fbo));
38         glCall(() => glGenRenderbuffers(1, &this.rbo));
39         glCall(() => glGenTextures(1, &this.texture));
40 
41         //Texture initialization
42         glCall(() => glBindFramebuffer(GL_FRAMEBUFFER, this.fbo));
43         glCall(() => glBindTexture(GL_TEXTURE_2D, this.texture));
44         glCall(() => glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, null));
45         glCall(() => glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
46         glCall(() => glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
47         //Attach to the framebuffer
48         glCall(() => glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this.texture, 0));
49 
50         //Render buffer initialization
51         glCall(() => glBindRenderbuffer(GL_RENDERBUFFER, this.rbo));
52 
53         version(HipGL3)
54         {
55             glCall(() => glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height));
56             glCall(() => glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, this.rbo));
57         }
58         else
59         {
60             glCall(() => glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, width, height));
61             glCall(() => glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, this.rbo));
62         }
63 
64 
65         //Check if creation went successful
66 
67         ErrorHandler.assertErrorMessage(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE,
68         "GL Framebuffer creation", "Framebuffer was unable to complete its creations");
69         retTexture = new Hip_GL3_Texture();
70         retTexture.textureID = texture;
71 
72         //Reset to defaults
73         glCall(() => glBindFramebuffer(GL_FRAMEBUFFER, 0));
74         glCall(() => glBindRenderbuffer(GL_RENDERBUFFER, 0));
75         glCall(() => glBindTexture(GL_TEXTURE_2D, 0));
76     }
77     void resize(uint width, uint height){}
78 
79     void bind(){glCall(() => glBindFramebuffer(GL_FRAMEBUFFER, this.fbo));}
80     void unbind(){glCall(() => glBindFramebuffer(GL_FRAMEBUFFER, 0));}
81     void clear()
82     {
83         glCall(() => glClearColor(0.0, 0.0, 0.0, 1.0));
84         glCall(() => glClear(GL_COLOR_BUFFER_BIT));
85     }
86 
87     IHipTexture getTexture(){return retTexture;}
88 
89     void draw()
90     {
91         // glBindTexture(GL_TEXTURE_2D, this.texture);
92         // glDrawArrays(GL_TRIANGLES, 0, 6);
93     }
94 
95     void dispose()
96     {
97         glCall(() => glDeleteTextures(1, &this.texture));
98         glCall(() => glDeleteFramebuffers(1, &this.fbo));
99         glCall(() => glDeleteRenderbuffers(1, &this.rbo));
100     }
101 }